//
//  LCRecommendCollection+LoadData.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/17.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCRecommendCollection+LoadData.h"

@implementation LCRecommendCollection (LoadData)

//tuijian
-(void)loadTuijianData
{
    YYCache *yyCache=[YYCache cacheWithName:@"LCVideoRecommendCache"];
    [yyCache.diskCache setCostLimit:10*1024*1024];//磁盘最大缓存开销
    [yyCache.diskCache setAutoTrimInterval:60*60*24*2];//设置磁盘lru动态清理频率 默认 60秒
    NSString *key= [NSString stringWithFormat:@"%@",@"keyHomeCache"];
    
    [RecommendNetWork loadLCRecommendCollectionWithSuccess:^(NSArray<LCRecommendHomeModel *> *arrayData) {
            self.videoManager = arrayData;
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.collectionView reloadData];//刷新界面
            }];
        
//        ///进行缓存
//        NSMutableArray *cacheMutableArray = [[NSMutableArray alloc] init];
//        for (NSInteger i = 0; i < self.videoManager.count; i++) {
//            LCVideoHomeModel *model = self.videoManager[i];
//            NSDictionary *json = [model yy_modelToJSONObject];
//            [cacheMutableArray addObject:json];
//        }
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
//            if (cacheMutableArray) {
//                [yyCache setObject:cacheMutableArray forKey:key];//添加数据到yyCache里缓存下来
//            }
//        });

    } failure:^(NSError *error) {
        NSLog(@"error = %@", error);
        ///网络错误提示
        [self netErrorWithStatus:NoNetwork];
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
//            if ([yyCache containsObjectForKey:key]) {
//                //根据key读取数据
//                id vuale = [yyCache objectForKey:key];
//                self.videoManager = [NSArray yy_modelArrayWithClass:[LCVideoHomeModel class] json:vuale].mutableCopy;
//            }
//            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
//                [self.collectionView reloadData];//刷新界面
//            }];
//        });

    }];
        
//    [kNetworkTool POSTFuYinWithUrlString:urlString input:parameters success:^(NSDictionary *dict) {
//        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
//        if ([status isEqualToString:@"1"]) {
//            NSArray *dictArr2 = dict[@"data"][@"tuijian"];
//            NSArray *dictArr3 = dict[@"data"][@"hot"];
//            NSArray *dictArr4 = dict[@"data"][@"news"];
//
//            NSMutableArray *mutableArray = [NSMutableArray array];
//            for (NSInteger i = 0; i < 3; i++) {
//                LCVideoHomeModel *videoHomeModel = [[LCVideoHomeModel alloc] init];
//                NSArray *mArray;
//                if (i == 0) {
//                    videoHomeModel.name = @"推荐";
//                    mArray =[NSArray yy_modelArrayWithClass:[LCVideoHomeListAboutModel class] json:dictArr2];
//                }
//                if (i == 1) {
//                    videoHomeModel.name = @"热门";
//                    mArray =[NSArray yy_modelArrayWithClass:[LCVideoHomeListAboutModel class] json:dictArr3];
//                }
//                if (i == 2) {
//                    videoHomeModel.name = @"最新";
//                    mArray =[NSArray yy_modelArrayWithClass:[LCVideoHomeListAboutModel class] json:dictArr4];
//                }
//                videoHomeModel.list = mArray;
//                [mutableArray addObject:videoHomeModel];
//            }
//            self.videoManager = mutableArray;
//            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
//                [self.collectionView reloadData];//刷新界面
//            }];
//        }
//
//        ///进行缓存
//        NSMutableArray *cacheMutableArray = [[NSMutableArray alloc] init];
//        for (NSInteger i = 0; i < self.videoManager.count; i++) {
//            LCVideoHomeModel *model = self.videoManager[i];
//            NSDictionary *json = [model yy_modelToJSONObject];
//            [cacheMutableArray addObject:json];
//        }
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
//            if (cacheMutableArray) {
//                [yyCache setObject:cacheMutableArray forKey:key];//添加数据到yyCache里缓存下来
//            }
//        });
//
//    } failure:^(NSError *error) {
//        ///网络错误提示
//        [self netErrorWithStatus:NoNetwork];
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
//            if ([yyCache containsObjectForKey:key]) {
//                //根据key读取数据
//                id vuale = [yyCache objectForKey:key];
//                self.videoManager = [NSArray yy_modelArrayWithClass:[LCVideoHomeModel class] json:vuale].mutableCopy;
//            }
//            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
//                [self.collectionView reloadData];//刷新界面
//            }];
//        });
//    }];
}
- (void)netErrorWithStatus:(NSString *)status {
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://baidu.com"];
    if ([reach currentReachabilityStatus] == NotReachable){
        [SVProgressHUD showErrorWithStatus:status];
        [SVProgressHUD dismissWithDelay:0.5];
    }
}


@end
